package com.example.his.api.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.DeptDao;
import com.example.his.api.db.pojo.DeptEntiy;
import com.example.his.api.mis.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service("MisDeptService")
public class DeptServiceImpl implements DeptService {
  
  private static final Logger log = LoggerFactory.getLogger(DeptServiceImpl.class);
  @Resource
  private DeptDao deptDao;
  
  @Override
  public ArrayList <HashMap> searchAllDept() {
    ArrayList <HashMap> list = deptDao.searchAllDept();
    
    return list;
  }
  
  
  @Override
  @Transactional
  public Integer insert(Map param) {
    DeptEntiy bean = BeanUtil.toBean(param, DeptEntiy.class);
    deptDao.insert(bean);
    Integer id = bean.getId();
    return id;
  }
  
  @Override
  @Transactional
  public int deleteByIds(Integer[] ids) {
    int i = deptDao.deleteByIds(ids);
    return i;
  }
  
  @Override
  @Transactional
  public boolean update(Map param) {
    int rows = deptDao.update(param);
    return rows == 1;
  }
  
  @Override
  public DeptEntiy searchById(Integer id) {
    DeptEntiy map = deptDao.searchById(id);
    return map;
  }
  
  @Override
  public PageUtils searchByPage(Map param) {
    ArrayList list = new ArrayList();
    long count = deptDao.searchCount(param);
    if(count > 0) {
      list = deptDao.searchByPage(param);
    }
    Integer page = MapUtil.getInt(param, "page");
    Integer length = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    return pageUtils;
  }
}
